package com.wang.tool.common;

import java.io.Serializable;
import java.util.Objects;

/**
 * 破java 闭包实现不完全,只能final的属性
 *
 * @Author shaopeng
 * @Date 2020-07-18
 */
public final class Container<T> implements Serializable {
    private static final long serialVersionUID = 150217256395848051L;

    private T obj;


    public static <D> Container<D> build(D obj) {
        return new Container<>(obj);
    }

    public Container(T obj) {
        this.obj = obj;
    }

    public Container() {
    }

    public boolean hasData() {
        return obj != null;
    }

    public T get() {
        return obj;
    }

    public void set(T t) {
        this.obj = t;
    }

    public T getObj() {
        return obj;
    }

    public void setObj(T obj) {
        this.obj = obj;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o){
            return true;
        }
        if (o == null || getClass() != o.getClass()){
            return false;
        }
        Container<?> container = (Container<?>) o;
        return Objects.equals(obj, container.obj);
    }

    @Override
    public int hashCode() {
        return Objects.hash(obj);
    }
}
